package com.scau.clothes.api.stock;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.entity.Category;
import com.scau.clothes.stock.model.entity.Norm;
import com.scau.clothes.stock.service.NormService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/norm")
@RestController
public class NormController {

    @Resource
    private NormService normService;

    @PostMapping("/listNormByColorAndSize")
    @ApiOperation("通过颜色和尺码查询规格")
    public AjaxResult listNormByColorAndSize(@RequestBody Norm norm){
        List<Norm> list = normService.listNormByColorAndSize(norm);
        return AjaxResult.success("查询成功",list);
    }
    @GetMapping("/selectNormByNormSn")
    @ApiOperation("通过规格编号查询规格")
    public AjaxResult selectNormByNsn(@RequestParam String normSn){
        Norm norm = normService.selectNormByNormSn(normSn);
        return norm != null ? AjaxResult.success("查询成功",norm) : AjaxResult.error("查询失败");
    }

    @PostMapping("/insertNorm")
    @ApiOperation("新增规格")
    public AjaxResult insertNorm(@RequestBody Norm norm){
        int rows = normService.insertNorm(norm);
        return rows > 0 ? AjaxResult.success("添加规格成功") : AjaxResult.error("添加规格失败");
    }

    @PostMapping("/updateNormInfo")
    @ApiOperation("修改规格信息")
    public AjaxResult updateNormInfo(@RequestBody Norm norm){
        int rows = normService.updateNorm(norm);
        return rows > 0 ? AjaxResult.success("修改规格信息成功") : AjaxResult.error("修改规格信息失败");
    }

    @PostMapping("/deleteNorm")
    @ApiOperation("删除规格")
    public AjaxResult deleteNorm(@RequestBody Norm norm){
        int rows = normService.deleteNorm(norm);
        return rows > 0 ? AjaxResult.success("删除规格成功") : AjaxResult.error("删除规格失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Norm norm){
        List<Norm> list=normService.listNormByColorAndSize(norm);
        Page page =new Page(pageNum,pageSize);
        int total=list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        PageHelper.startPage(pageNum,pageSize);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}

